using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.CodeDom.Compiler;

namespace Lds.Ward.PhotoDirectory.Logic
{
	public enum MediaQuality
	{
		WardWebSite,
		Screen,
		Print,
		Maximum,
	}

	public class FamiliesToDirectoryPdf : FamiliesToPdf
	{
		public FamiliesToDirectoryPdf(IEnumerable<Family> families, string unitName)
			: base(families, unitName, "/XmlToDirectoryFO.xslt")
		{
		}

		public static void Convert(IEnumerable<Family> families, string unitName,
			Uri photosDirectory,
			string normalAreaCode, PostalAddress normalAddress, Stream pdf, MediaQuality quality,
			org.apache.fop.apps.Driver.UpdateStatusCallback update)
		{
			FamiliesToDirectoryPdf c = new FamiliesToDirectoryPdf(families, unitName);
			c.Convert(photosDirectory, normalAreaCode, normalAddress, pdf, quality, update);
		}

		public void Convert(Uri photosDirectory,
			string normalAreaCode, PostalAddress normalAddress, Stream pdf, MediaQuality quality,
			org.apache.fop.apps.Driver.UpdateStatusCallback update)
		{
			if (pdf == null) throw new ArgumentNullException("pdf");

			using (TempFileCollection tfc = new TempFileCollection())
			{
				tfc.KeepFiles = false;

				XmlReader rawXml = GetXml(photosDirectory, tfc, quality);
				XmlReader filtered = GetFilteredXml(rawXml, normalAddress, normalAreaCode);
				XmlTextReader fo = GetFO(filtered);
				FOtoPdf(fo, pdf, update);
			}
		}
	}
}
